<?php

/**
 * This is the model class for table "Video".
 *
 * The followings are the available columns in table 'Video':
 * @property string $video_id
 * @property string $title
 * @property string $description
 * @property string $code
 * @property string $url
 *
 * The followings are the available model relations:
 * @property Petvideo[] $petvideos
 * @property Uservideo[] $uservideos
 * @property Videocomment[] $videocomments
 */
class Video extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Video the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Video';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('title, url', 'length', 'max'=>255),
			array('code', 'length', 'max'=>20),
			array('description', 'safe'),
                        array('code, url', 'codeOrUrlRule'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('video_id, title, description, code, url', 'safe', 'on'=>'search'),
		);
	}

        public function codeOrUrlRule($attribute,$params)
        {
            if ($attribute == 'url')
            {
                return;
            }
            if (empty($this->code) && empty($this->url))
            {
                $this->addError('code','Укажите ссылку или код на видео.');
                $this->addError('url','');
            }
        }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'petvideos' => array(self::HAS_MANY, 'Petvideo', 'video_id'),
			'uservideos' => array(self::HAS_MANY, 'Uservideo', 'video_id'),
			'comment_flow' => array(self::HAS_ONE, 'commentflow', 'comment_flow_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'video_id' => 'Video',
			'title' => 'Название',
			'description' => 'Описание',
			'code' => 'Код видео',
			'url' => 'Url ссылка',
                        'publish_date' => 'Дата',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('video_id',$this->video_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('url',$this->url,true);
                $criteria->compare('publish_date',$this->publish_date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}